package com.bbx.taxi.client.widget.pop;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.ShareDetailBuild;
import com.bbx.api.sdk.model.passanger.SharelinkBuild;
import com.bbx.api.sdk.model.passanger.Return.ShareDetail;
import com.bbx.api.sdk.model.passanger.Return.Sharelink;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.ShareDetailNet;
import com.bbx.api.sdk.net.passeger.conn.SharelinkNet;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Util.AppStarted;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.example.loadview.LoadingDialog;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class SharePopView implements OnClickListener{
	/**
	 * 获取获得详情结束调用
	 */
	public interface OnShareDetaiFinish{
		/**
		 * 获取获得详情结束调用，sum不为0为，成功
		 */
		public void onShareDetaiFinish(String sum);
	}
	public OnShareDetaiFinish mOnShareDetaiFinish;
	public void setOnShareDetaiFinish(OnShareDetaiFinish mOnShareDetaiFinish) {
		this.mOnShareDetaiFinish = mOnShareDetaiFinish;
	}
	/**阴影布局*/
	private  View pop_background;
	/** 1为微信，2为微博 */
	private int share_type;
	private int click;
	private final int TYPE_WEIXIN = 1;
	private final int TYPE_WEIBO = 2;
	public String sum="0";
	private  Activity context;
	public PopupWindow popview;
	private LoadingDialog dialog;
	private MyApplication mApplication;
	private SharelinkTask mSharelinkTask;
	private ShareDetailTask mShareDetailTask;
	private UMSocialService mController;
	public static SharePopView mSharePopView;
	public SharePopView(Activity context,View pop_background) {
		this.context = context;
		this.pop_background=pop_background;
		mApplication = MyApplication.getInstance();
		mController = UMServiceFactory
				.getUMSocialService(AppStarted.getAppPackageName(context));
		addQQQZonePlatform();
		addWXPlatform();
		addSinaPlatform();
		initPopView();
	}

	public void initPopView() {
		View rootView = LayoutInflater.from(context).inflate(
				R.layout.pop_recommen, null);
		popview = new PopupWindow(rootView, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		popview.setBackgroundDrawable(new BitmapDrawable());
		popview.setContentView(rootView);
		popview.setOutsideTouchable(true);
		popview.setFocusable(true);
		popview.setAnimationStyle(R.style.mypopwindow_anim_style);
		rootView.findViewById(R.id.btn_share_wx).setOnClickListener(this);
		rootView.findViewById(R.id.btn_share_pyq).setOnClickListener(this);
		rootView.findViewById(R.id.btn_share_qq).setOnClickListener(this);
		rootView.findViewById(R.id.btn_share_wb).setOnClickListener(this);
		rootView.findViewById(R.id.btn_cancel).setOnClickListener(this);
		popview.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				pop_background.setVisibility(View.GONE);
			}
		});
	}
	public void onShow(View v) {
		pop_background.setVisibility(View.VISIBLE);
		popview.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}
	public UMSocialService getController(){
		return mController;
		
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_share_wx:
			click = 0;
			getSharelink(TYPE_WEIXIN);
			// performShare(SHARE_MEDIA.WEIXIN);
			break;
		case R.id.btn_share_pyq:
			click = 1;
			getSharelink(TYPE_WEIXIN);
			// performShare(SHARE_MEDIA.WEIXIN_CIRCLE);
			break;
		case R.id.btn_share_qq:
			click = 2;
			// performShare(SHARE_MEDIA.QQ);
			break;
		case R.id.btn_share_wb:
			click = 3;
			getSharelink(TYPE_WEIBO);
			// performShare(SHARE_MEDIA.SINA);
			break;
		case R.id.btn_cancel:
			popview.dismiss();
			break;
		}
	}
	/**
	 * 获取分享链接
	 */
	public void getSharelink(int type) {
		
		this.share_type = type;
		SharelinkBuild mSharelinkBuild = new SharelinkBuild(context);
		mSharelinkBuild.uid = mApplication.getUid();
		mSharelinkBuild.access_token = mApplication.getToken();
		mSharelinkBuild.platform = type;
		mSharelinkBuild.type = Value.RecommenType;
		mSharelinkTask = new SharelinkTask(context, mSharelinkBuild);
		mSharelinkTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	/**
	 * 获取活动详情
	 */
	public void getShareDetai() {
		dialog = new LoadingDialog(context);
		ShareDetailBuild mShareDetailBuild = new ShareDetailBuild(context);
		mShareDetailBuild.uid = mApplication.getUid();
		mShareDetailBuild.access_token = mApplication.getToken();
		mShareDetailBuild.type = Value.RecommenType;
		mShareDetailTask = new ShareDetailTask(context, mShareDetailBuild);
		mShareDetailTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		if (dialog != null && !dialog.isShowing()) {
			dialog.show();
		}
	}
	/**
	 * 获取活动详情
	 */
	private class ShareDetailTask extends BaseAsyncTask {

		private ShareDetailBuild mShareDetailBuild;
		public ShareDetailTask(Activity context,
				ShareDetailBuild mShareDetailBuild) {
			super(context,false);
			this.mShareDetailBuild = mShareDetailBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				ShareDetail mShareDetail=(ShareDetail)object;
				if(mShareDetail!=null){
					sum=mShareDetail.getSum()!=null?mShareDetail.getSum():"0";
					if(mOnShareDetaiFinish!=null)mOnShareDetaiFinish.onShareDetaiFinish(sum);
				}else{
					ToastUtil.showToast(context, context.getString(R.string.no_network));
				}

			}
			if(dialog!=null){
				dialog.onDismiss();
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, context.getString(R.string.no_network));
			if(dialog!=null){
				dialog.onDismiss();
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mShareDetailBuild).getJson();
			return new ShareDetailNet(context, json);
		}
	}
	/**
	 * 获取URI
	 */
	private class SharelinkTask extends BaseAsyncTask {

		private SharelinkBuild mSharelinkBuild;

		public SharelinkTask(Activity context, SharelinkBuild mSharelinkBuild) {
			super(context);
			this.mSharelinkBuild = mSharelinkBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Sharelink mSharelink = (Sharelink) object;

				switch (share_type) {
				case TYPE_WEIXIN:
					switch (click) {
					case 0:
						performShare(SHARE_MEDIA.WEIXIN, mSharelink.getUrl());
						break;
					case 1:
						performShare(SHARE_MEDIA.WEIXIN_CIRCLE,
								mSharelink.getUrl());
						break;
					}

					break;
				case TYPE_WEIBO:
					performShare(SHARE_MEDIA.SINA, mSharelink.getUrl());
					break;
				}
			}

		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context,
					context.getResources().getString(R.string.no_network));
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mSharelinkBuild).getJson();
			return new SharelinkNet(context, json);
		}
	}
	/**
	 * 分享到微信
	 */
	private void performShare(SHARE_MEDIA platform, String uri) {
		if (platform == SHARE_MEDIA.WEIXIN) {
			WeiXinShareContent media = new WeiXinShareContent(new UMImage(context,
					R.drawable.ic_launcher));
			media.setTitle(String.format(context.getString(R.string.share_title), sum));
			media.setShareContent(String.format(context.getString(R.string.share_content), sum));
			media.setTargetUrl(uri);
			mController.setShareMedia(media);
		} else if (platform == SHARE_MEDIA.WEIXIN_CIRCLE) {

			CircleShareContent media = new CircleShareContent(new UMImage(context,
					R.drawable.ic_launcher));
			media.setTitle(String.format(context.getString(R.string.share_title), sum));
			media.setShareContent(String.format(context.getString(R.string.share_content), sum));
			media.setTargetUrl(uri);
			mController.setShareMedia(media);
		} else if (platform == SHARE_MEDIA.SINA) {
			mController.getConfig().setSsoHandler(new SinaSsoHandler());
			SinaShareContent media = new SinaShareContent(new UMImage(context,
					R.drawable.ic_launcher));
			media.setTitle(String.format(context.getString(R.string.share_title), sum));
			media.setShareContent(String.format(context.getString(R.string.share_content)+uri, sum));
			media.setTargetUrl(uri);
			mController.setShareMedia(media);

		}
		mController.postShare(context, platform, new SnsPostListener() {
			@Override
			public void onStart() {
				ToastUtil.showToast(context, R.string.start_share);
			}

			@Override
			public void onComplete(SHARE_MEDIA platform, int eCode,
					SocializeEntity entity) {
				if (eCode == 200) {
					ToastUtil.showToast(context, R.string.share_success);
				} else {
					String msg = "";
					if (eCode == 200) {
						msg =context. getString(R.string.no_authorization);
					} else {

					}
					ToastUtil.showToast(context, R.string.share_fail);
				}
			}
		});
	}
	private void addWXPlatform() {
		// 注意：在微信授权的时候，必须传递appSecret
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = Value.weixin_appId;
		String appSecret = Value.weixin_appSecret;
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(context, appId, appSecret);
		wxHandler.addToSocialSDK();

		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(context, appId, appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();

	}
	private void addQQQZonePlatform() {
		String appId = Value.qq_appId;
		String appKey = Value.qq_appKey;
		// 添加QQ支持, 并且设置QQ分享内容的target url
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(context, appId, appKey);
		qqSsoHandler.setTargetUrl("https://www.baidu.com/");
		qqSsoHandler.addToSocialSDK();
	}

	private void addSinaPlatform() {
		// // 添加新浪SSO授权
		// mController.getConfig().setSsoHandler(new SinaSsoHandler());
		// SinaShareContent sinaContent = new SinaShareContent();
		// sinaContent.setShareContent("分享帮邦行");
		// // sinaContent.setShareImage( new UMImage( getActivity(),
		// // R.drawable.actionbar_back_indicator));
		// mController.setShareMedia(sinaContent);
	}
}
